package com.hymall.controller.wx;

import com.hymall.bean.MarketCart;
import com.hymall.bean.MarketUser;
import com.hymall.bean.bo.*;
import com.hymall.bean.common.BaseRespVo;
import com.hymall.bean.vo.WxCartCheckoutVO;
import com.hymall.bean.vo.WxCartIndexVO;
import com.hymall.bean.vo.WxCartTotalVO;
import com.hymall.service.wx.WxCartService;
import com.hymall.util.WxCartShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @Description 业务层cart
 * @Author 徐成喜
 * @Data 2022/7/19 10:08
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartIndexController {

    //todo:等shiro写好整合，暂时默认

   /* static Integer userId = 1;*/

    @Autowired
    WxCartService wxCartService;

    /**
     * @return
     * @description 实现点开购物车返回json对象显示功能
     */
    @RequestMapping("index")
    public BaseRespVo index() {
        Integer userId = WxCartShiroUtils.getUserId();
        if(userId==null){
            return BaseRespVo.error(501,"请登录");
        }
        WxCartIndexVO data = wxCartService.wxCartIndex(userId);
        return BaseRespVo.ok(data);

    }

    @RequestMapping("goodscount")
    public BaseRespVo goodscount() {
        Integer userId = WxCartShiroUtils.getUserId();
        if(userId==null){
            return BaseRespVo.ok(0);
        }
        WxCartIndexVO wxCartIndexVO = wxCartService.wxCartGoodscount(userId);
        Integer data = wxCartIndexVO.getCartTotal().getGoodsCount();
        return BaseRespVo.ok(data);

    }

    /**
     * @return
     * @description 实现加入购物车返回json对象显示功能
     */
    @PostMapping("add")
    public BaseRespVo add(@RequestBody WxCartAddBO wxCartAddBO) {
        Integer userId = WxCartShiroUtils.getUserId();
        if(userId==null){
            return BaseRespVo.error(501,"请登录");
        }
        WxCartIndexVO wxCartIndexVO = wxCartService.wxCartAdd(userId, wxCartAddBO);
        Integer data = wxCartIndexVO.getCartTotal().getGoodsCount();
        return BaseRespVo.ok(data);

    }


    /**
     * @return 返回json字符串
     * @description 实现购物车下单功能
     */
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody WxCartIsCheckBO checkBO) {
        Integer userId = WxCartShiroUtils.getUserId();
        WxCartIndexVO data = wxCartService.wxCartChecked(userId, checkBO);
        return BaseRespVo.ok(data);

    }

    /**
     * @return 修改，返回
     * @description 实现购物车修改数量功能
     */

    @PostMapping("update")
    public BaseRespVo update(@RequestBody WxCartUpdateBO updateBO) {
        Integer userId = WxCartShiroUtils.getUserId();
        wxCartService.wxCartUpdate(userId, updateBO);
        return BaseRespVo.ok();

    }


    /**
     * @return a
     * @description 实现购物车删除商品功能
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody WxCartProductBO productBO) {
        Integer userId = WxCartShiroUtils.getUserId();
        WxCartIndexVO data = wxCartService.wxCartDelete(userId, productBO);
        WxCartTotalVO cartTotal = data.getCartTotal();
        cartTotal.setCheckedGoodsCount(0);

        return BaseRespVo.ok(data);

    }


    /**
     * @return 返回cart表中id
     * @description 页面中立即购买
     */
    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody WxCartAddBO wxCartAddBO) {
        Integer userId = WxCartShiroUtils.getUserId();
        if(userId==null){
            return BaseRespVo.error(501,"请登录");
        }
        MarketCart marketCart = wxCartService.wxCartFastadd(userId, wxCartAddBO);
        return BaseRespVo.ok(marketCart.getId());

    }

    /**
     * @return
     * @description 购物车下单
     */

    @RequestMapping("checkout")

    public BaseRespVo checkout(WxCartCheckoutBO checkoutBO) throws Exception {
        Integer userId = WxCartShiroUtils.getUserId();
        if(userId==null){
            return BaseRespVo.error(501,"请登录");
        }
        WxCartCheckoutVO checkoutVO = wxCartService.wxCartCheckout(userId, checkoutBO);
        return BaseRespVo.ok(checkoutVO);
    }


}
